Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

450
Visualizações
Updating State | object is not iterable (cannot read property Symbol(Symbol.iterator))

I have below JSX -

 <FormGroup>
                <Label for="exampleEmail" style={{fontWeight:"bold"}}>What is your e-mail address?</Label>
                <Input type="email" name="email" id="exampleEmail" onChange={(e)=>setStateForProperties(e)} />
            </FormGroup>
            <FormGroup>
                <Label for="examplePassword" style={{fontWeight:"bold"}}>What is your password?</Label>
                <Input type="password" name="password" id="examplePassword" onChange={(e)=>setStateForProperties(e)}/>
 </FormGroup>

State Declarations -

  const iLoginCreds ={
    userName:'',
    password:''
  }
  const [loginCreds, setLoginCredentials] = useState(iLoginCreds)

onChange Method changing state -

  function setStateForProperties(e)
  {
    alert(e.target.id);
    switch (e.target.id) {
      case 'exampleEmail':
        setLoginCredentials(...loginCreds,{
          userName:e.target.value
        
        });
            
        break;

        case 'examplePassword':
        setLoginCredentials(... loginCreds,{
          password:e.target.value
        });
    
      default:
        break;
    }
    console.log(loginCreds);
  }

Error -

When I write anything in textbox (upon onchange event trigger) , I receive an error -

TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))

enter image description here

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to destruct the previous object properties and return a new object after merging with the new one.

setLoginCredentials({ ...loginCreds, ...{userName:e.target.value} })
about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of doing this:

setLoginCredentials(...loginCreds, {
  userName:e.target.value
});

, which passes in 2 arguments to useState, you should move the curly braces to surround ...loginCreds, like so:

setLoginCredentials({...loginCreds,
  userName:e.target.value
});

You should do the same for password.

This will change only one property on the object. React Hooks useState() with Object

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda